class Animal(object):
    def run(self):
        print('Animal is running...')

class Dog(Animal):
    pass



#Dog类继承了Animal类，从而获得父类的方法run()：
dog = Dog()
dog.run()

'''
多态
'''
class Dog(Animal):
    def run(self):
        print('Dog is runing')
class Cat(Animal):
    def run(self):
        print('Cat is runing')
def runClass(obj):
    obj.run()
runClass(Animal())
runClass(Dog())
runClass(Cat())

print("--------------------------------")

'''
类属性和实例属性
'''

a = Animal()
a.name = 'dog'


'''
获取对象信息
'''

print(type(1))
print(type('123'))
print(type(a))

print(type(1) == int)
print(type('123') == str)
print(type(a) == Animal)


print("判断类的继承关系")
dog = Dog()
print(isinstance(dog, Dog))
print(isinstance(dog,object))

print("获取一个对象的所有属性和方法")
print(dir(dog))

print("--------------------------------------------------------------")

class Animal(object):
    def __init__(self):
        self.type = 'animal'
        self.area = 'China'
    def run(self):
        print('runing')
class Dog(Animal):
    pass
dog = Dog()
# getattr() 获取对象的属性
# setattr() 可以设置类的某个属性
# hasattr() 可以判断对象的属性
print(hasattr(dog,'type'))
print(hasattr(dog,'run'))
print(hasattr(dog,'name'))
print(getattr(dog,'area'))

setattr(dog, 'name', 'Aia')
print(getattr(dog,'name'))

# getattr()尝试获取对象的一个不存在属性会报错。可以通过设置默认值避免：
print(getattr(dog,'name',404))
